package com.linksfield.topup.controller.comlinkmanage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.linksfield.topup.config.dto.ResultDataDto;
import com.linksfield.topup.config.dto.StringOutputConstant;
import com.linksfield.topup.entity.interfaces.comlinkmanage.create.first.request.RequestFirstPlanCreate;
import com.linksfield.topup.entity.interfaces.comlinkmanage.create.fourth.request.RequestFourthPlanCreate;
import com.linksfield.topup.entity.interfaces.comlinkmanage.create.second.request.RequestSecondPlanCreate;
import com.linksfield.topup.entity.interfaces.comlinkmanage.create.third.request.RequestThirdPlanCreate;
import com.linksfield.topup.entity.interfaces.comlinkmanage.edit.context.first.request.RequestFirstContentEdit;
import com.linksfield.topup.entity.interfaces.comlinkmanage.edit.context.fourth.request.RequestFourthPayEdit;
import com.linksfield.topup.entity.interfaces.comlinkmanage.edit.context.second.request.RequestSecondContentEdit;
import com.linksfield.topup.entity.interfaces.comlinkmanage.edit.context.third.request.RequestThirdContentEdit;
import com.linksfield.topup.entity.interfaces.comlinkmanage.edit.langtext.first.request.RequestFirstLangEdit;
import com.linksfield.topup.entity.interfaces.comlinkmanage.edit.langtext.fourth.request.RequestFourthLangEdit;
import com.linksfield.topup.entity.interfaces.comlinkmanage.edit.langtext.second.request.RequestSecondLangEdit;
import com.linksfield.topup.entity.interfaces.comlinkmanage.edit.langtext.third.request.RequestThirdLangEdit;
import com.linksfield.topup.entity.interfaces.comlinkmanage.langlist.request.RequestLangList;
import com.linksfield.topup.entity.interfaces.comlinkmanage.langlist.response.ResponseLangList;
import com.linksfield.topup.entity.interfaces.comlinkmanage.operation.request.RequestPlanOperation;
import com.linksfield.topup.entity.interfaces.comlinkmanage.search.request.RequestContentPlanSearch;
import com.linksfield.topup.entity.interfaces.comlinkmanage.search.response.ResponseContentPlanSearch;
import com.linksfield.topup.entity.interfaces.comlinkmanage.view.request.RequestContentPlanView;
import com.linksfield.topup.entity.interfaces.comlinkmanage.view.response.first.ResponseFirstPlanView;
import com.linksfield.topup.entity.interfaces.comlinkmanage.view.response.fourth.ResponseFourthPlanView;
import com.linksfield.topup.entity.interfaces.comlinkmanage.view.response.second.ResponseSecondPlanView;
import com.linksfield.topup.entity.interfaces.comlinkmanage.view.response.third.ResponseThirdPlanView;
import com.linksfield.topup.service.PartnerConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLDecoder;

/**
 * 管理员为普通企业配置语言等内容方案等功能的控制器
 *
 * @author: lm
 * @date: 2019-12-24 10:24
 */
@RestController
@Api(value = "H5功能管理")

public class PartnerContentConfigController {

    private static Logger logger = Logger.getLogger(PartnerContentConfigController.class);

    @Autowired
    PartnerConfigService configService;


    @PostMapping(value = "contentPlanSearch", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：内容方案搜索", response = ResponseContentPlanSearch.class)
    public ResultDataDto contentPlanSearch(@ApiParam @RequestBody RequestContentPlanSearch request,
                                           @RequestHeader(name = "x-access-token", required = false) String token) {
        try {
            logger.info("【H5功能管理：内容方案搜索】请求：" + JSON.toJSONString(request));
            if (StringUtils.isBlank(token)) {
                return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
            }
            return configService.contentPlanSearch(request, token);
        } catch (Exception e) {
            logger.info("请求报文格式不是json格式：" + e);
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_JSON_PARAM_ERROR);
        }

    }


    @PostMapping(value = "firstPlanCreate", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：新增H5服务方案：方案一")
    public ResultDataDto firstPlanCreate(@RequestParam(value = "param", required = false) String param,
                                         MultipartFile fileListPc,
                                         MultipartFile fileListPh,
                                         @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：新增H5服务方案：方案一】请求：" + param);

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        RequestFirstPlanCreate request = null;

        try {
            request = JSONObject.parseObject(param, RequestFirstPlanCreate.class);

        } catch (Exception e) {
            logger.info("请求报文格式不是json格式：" + e);
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_JSON_PARAM_ERROR);
        }

        if (null != fileListPc && !fileListPc.isEmpty() && null != fileListPh && !fileListPh.isEmpty()) {
            logger.info("方案一pc图片名称：" + fileListPc.getOriginalFilename());
            logger.info("方案一ph图片名称：" + fileListPh.getOriginalFilename());
        }
        return configService.firstPlanCreate(request, fileListPc, fileListPh, token);
    }


    @PostMapping(value = "secondPlanCreate", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：新增H5服务方案：方案二")
    public ResultDataDto secondPlanCreate(@RequestParam(value = "param", required = false) String param,
                                          MultipartFile fileListPc, MultipartFile fileListPh,
                                          @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：新增H5服务方案：方案二】请求：" + param);
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        RequestSecondPlanCreate request = null;
        try {
            request = JSONObject.parseObject(param, RequestSecondPlanCreate.class);

        } catch (JSONException e) {
            logger.info("请求报文格式不是json格式：" + e);
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_JSON_PARAM_ERROR);
        }
        if (null != fileListPc && !fileListPc.isEmpty() && null != fileListPh && !fileListPh.isEmpty()) {
            logger.info("方案二pc图片名称：" + fileListPc.getOriginalFilename());
            logger.info("方案二ph图片名称：" + fileListPh.getOriginalFilename());
        }
        return configService.secondPlanCreate(request, fileListPc, fileListPh, token);

    }


    @PostMapping(value = "thirdPlanCreate", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：新增H5服务方案：方案三")
    public ResultDataDto thirdPlanCreate(@RequestParam(value = "param", required = false) String param,
                                         MultipartFile fileListPc, MultipartFile fileListPh,
                                         @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：新增H5服务方案：方案三】请求：" + param);
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        RequestThirdPlanCreate request = null;
        try {
            request = JSONObject.parseObject(param, RequestThirdPlanCreate.class);

        } catch (JSONException e) {
            logger.info("请求报文格式不是json格式：" + e);
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_JSON_PARAM_ERROR);
        }
        if (null != fileListPc && !fileListPc.isEmpty() && null != fileListPh && !fileListPh.isEmpty()) {
            logger.info("方案三pc图片名称：" + fileListPc.getOriginalFilename());
            logger.info("方案三ph图片名称：" + fileListPh.getOriginalFilename());
        }

        return configService.thirdPlanCreate(request, fileListPc, fileListPh, token);
    }


    @PostMapping(value = "fourthPlanCreate", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：新增H5服务方案：方案四")
    public ResultDataDto fourthPlanCreate(@RequestParam(value = "param", required = false) String param,
                                          MultipartFile fileListPc, MultipartFile fileListPh,
                                          @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：新增H5服务方案：方案四】请求：" + param);
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        RequestFourthPlanCreate request = null;
        try {
            request = JSONObject.parseObject(param, RequestFourthPlanCreate.class);

        } catch (JSONException e) {
            logger.info("请求报文格式不是json格式：" + e);
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_JSON_PARAM_ERROR);
        }
        if (null != fileListPc && !fileListPc.isEmpty() && null != fileListPh && !fileListPh.isEmpty()) {
            logger.info("方案四pc图片名称：" + fileListPc.getOriginalFilename());
            logger.info("方案四ph图片名称：" + fileListPh.getOriginalFilename());
        }

        return configService.fourthPlanCreate(request, fileListPc, fileListPh, token);
    }


    @PostMapping(value = "firstPlanView", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：查看：方案一", response = ResponseFirstPlanView.class)
    public ResultDataDto firstPlanView(@ApiParam @RequestBody RequestContentPlanView request,
                                       @RequestHeader(name = "x-access-token", required = false) String token) {
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        logger.info("【H5功能管理：查看：方案一】请求：" + JSON.toJSONString(request));
        return configService.firstPlanView(request, token);
    }


    @PostMapping(value = "secondPlanView", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：查看：方案二", response = ResponseSecondPlanView.class)
    public ResultDataDto secondPlanView(@ApiParam @RequestBody RequestContentPlanView request,
                                        @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：查看：方案二】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }

        return configService.secondPlanView(request, token);
    }


    @PostMapping(value = "thirdPlanView", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：查看：方案三", response = ResponseThirdPlanView.class)
    public ResultDataDto thirdPlanView(@ApiParam @RequestBody RequestContentPlanView request,
                                       @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：查看：方案三】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return configService.thirdPlanView(request, token);
    }


    @PostMapping(value = "fourthPlanView", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：查看：方案四", response = ResponseFourthPlanView.class)
    public ResultDataDto fourthPlanView(@ApiParam @RequestBody RequestContentPlanView request,
                                        @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：查看：方案四】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return configService.fourthPlanView(request, token);

    }


    @PostMapping(value = "langList", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：所有语种服务列表", response = ResponseLangList.class)
    public ResultDataDto langList(@ApiParam @RequestBody RequestLangList request,
                                  @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：所有语种服务列表】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return configService.langList(request, token);

    }


    @PostMapping(value = "firstLangEdit", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：方案一：语言编辑")
    public ResultDataDto firstLangEdit(@ApiParam @RequestBody RequestFirstLangEdit request,
                                       @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：方案一：语言编辑】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }

        return configService.firstLangEdit(request, token);
    }

    @PostMapping(value = "firstContentEdit", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：方案一：内容编辑")
    public ResultDataDto firstContentEdit(@RequestParam(value = "param", required = false) String param,
                                          @RequestParam(value = "fileListPc", required = false) MultipartFile fileListPc,
                                          @RequestParam(value = "fileListPh", required = false) MultipartFile fileListPh,
                                          @RequestHeader(name = "x-access-token", required = false) String token) {

        logger.info("【H5功能管理：方案一：内容编辑】请求：" + param);

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }

        RequestFirstContentEdit request = null;
        try {
            request = JSONObject.parseObject(param, RequestFirstContentEdit.class);
        } catch (JSONException e) {
            logger.info("请求报文格式不是json格式：" + e);
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_JSON_PARAM_ERROR);
        }
        if (null != fileListPc && !fileListPc.isEmpty() && null != fileListPh && !fileListPh.isEmpty()) {
            logger.info("方案一内容编辑pc图片名称：" + fileListPc.getOriginalFilename());
            logger.info("方案一内容编辑ph图片名称：" + fileListPh.getOriginalFilename());
        }
        return configService.firstContentEdit(request, token, fileListPc, fileListPh);

    }


    @PostMapping(value = "secondLangEdit", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：方案二：语言编辑")
    public ResultDataDto secondLangEdit(@ApiParam @RequestBody RequestSecondLangEdit request,
                                        @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：方案一：语言编辑】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return configService.secondLangEdit(request, token);
    }

    @PostMapping(value = "bgImgEdit", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理方案二内容编辑：仅背景图片编辑")
    public ResultDataDto bgImgEdit(@RequestParam(value = "param", required = false) String param,
                                   @RequestParam(value = "fileListPc", required = false) MultipartFile fileListPc,
                                   @RequestParam(value = "fileListPh", required = false) MultipartFile fileListPh,
                                   @RequestHeader(name = "x-access-token", required = false) String token) {
        //如果是这种传值：@RequestParam("files") MultipartFile[] files

        logger.info("【H5功能管理：方案二：背景图片编辑】请求：" + param);

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        RequestSecondContentEdit request = null;
        try {
            request = JSONObject.parseObject(param, RequestSecondContentEdit.class);
        } catch (JSONException e) {
            logger.info("请求报文格式不是json格式：" + e);
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_JSON_PARAM_ERROR);
        }
        if (null != fileListPc && !fileListPc.isEmpty() && null != fileListPh && !fileListPh.isEmpty()) {
            logger.info("方案二编辑pc图片名称：" + fileListPc.getOriginalFilename());
            logger.info("方案二编辑ph图片名称：" + fileListPh.getOriginalFilename());
            return configService.secondContentEdit(request, token, fileListPc, fileListPh);
        } else {
            return ResultDataDto.addOperationFailure("请选择相应背景图片再提交！");
        }
    }


    @PostMapping(value = "thirdLangEdit", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：方案三：语言编辑")
    public ResultDataDto thirdLangEdit(@ApiParam @RequestBody RequestThirdLangEdit request,
                                       @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：方案三：语言编辑】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return configService.thirdLangEdit(request, token);

    }


    @PostMapping(value = "thirdContentEdit", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：方案三：内容编辑")
    public ResultDataDto thirdContentEdit(@RequestParam(value = "param", required = false) String param,
                                          MultipartFile fileListPc,
                                          MultipartFile fileListPh,
                                          @RequestHeader(name = "x-access-token", required = false) String token) {

        logger.info("【H5功能管理：方案三：内容编辑】请求：" + param);
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        RequestThirdContentEdit request = null;
        try {
            request = JSONObject.parseObject(param, RequestThirdContentEdit.class);
        } catch (JSONException e) {
            logger.info("请求报文格式不是json格式：" + e);
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_JSON_PARAM_ERROR);
        }
        if (null != fileListPc && !fileListPc.isEmpty() && null != fileListPh && !fileListPh.isEmpty()) {
            logger.info("方案三内容编辑pc图片名称：" + fileListPc.getOriginalFilename());
            logger.info("方案三内容编辑ph图片名称：" + fileListPh.getOriginalFilename());
        }
        return configService.thirdContentEdit(request, token, fileListPc, fileListPh);
    }


    @PostMapping(value = "fourthLangEdit", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：方案四：语言编辑")
    public ResultDataDto fourthLangEdit(@ApiParam @RequestBody RequestFourthLangEdit request,
                                        @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：方案四：语言编辑】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return configService.fourthLangEdit(request, token);
    }


    @PostMapping(value = "fourthContentEdit", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：方案四：支付配置编辑")
    public ResultDataDto fourthPayEdit(@RequestParam(value = "param", required = false) String param,
                                       MultipartFile fileListPc,
                                       MultipartFile fileListPh,
                                       @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：方案四内容编辑】请求：" + param);
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }

        RequestFourthPayEdit request = null;
        try {
            request = JSONObject.parseObject(param, RequestFourthPayEdit.class);
        } catch (JSONException e) {
            logger.info("请求报文格式不是json格式：" + e);
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_JSON_PARAM_ERROR);
        }
        if (null != fileListPc && !fileListPc.isEmpty() && null != fileListPh && !fileListPh.isEmpty()) {
            logger.info("方案四内容编辑pc图片名称：" + fileListPc.getOriginalFilename());
            logger.info("方案四内容编辑ph图片名称：" + fileListPh.getOriginalFilename());
        }
        return configService.fourthContentEdit(request, token, fileListPc, fileListPh);

    }


    @PostMapping(value = "contentPlanOpen", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：方案启用")
    public ResultDataDto contentPlanOpen(@ApiParam @RequestBody RequestPlanOperation request,
                                         @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：方案启用】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return configService.contentPlanOpen(request, token);
    }

    @PostMapping(value = "contentPlanStop", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "H5功能管理：方案停用")
    public ResultDataDto contentPlanStop(@ApiParam @RequestBody RequestPlanOperation request,
                                         @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【H5功能管理：方案停用】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return configService.contentPlanStop(request, token);
    }

}
